﻿using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace DimensionsHelper.LanguageService.Syntax;

/// <summary>
///     参数宏调用表达式
/// </summary>
public class MacroCallExpression : PreExpression
{
    private readonly SourceNode _callee;
    private MacroArgumentList? _arguments;

    internal MacroCallExpression(SourceNode? parent, SourceNode callee) :
        base(parent, SyntaxKinds.MacroCallExpression)
    {
        SetChildField(ref _callee, callee);
    }


    /// <summary>
    ///     函数调用节点，此属性的节点类型是<see cref="SyntaxKinds.Identifier" />
    /// </summary>
    public SourceNode Callee => _callee;


    public MacroArgumentList? Arguments => _arguments;


    [MemberNotNullWhen(true, nameof(Arguments))]
    public bool HasArgument => Arguments != null;


    public MacroCallExpression Update(MacroArgumentList argumentList)
    {
        Debug.Assert(argumentList.IsNode(SyntaxKinds.MacroArgumentList));
        SetChildNullableField(ref _arguments, argumentList);
        return this;
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        Accept(visitor, Callee, Arguments);
    }
}


public static partial class NodeFactory
{
    public static MacroCallExpression CreateMacroCallExpression(SourceNode? parent, SourceNode callee,
        MacroArgumentList? argumentList)
    {
        Debug.Assert(callee is { IsIdentifier: true, IsEmpty: false });
        MacroCallExpression expr = new(parent, callee);
        if (argumentList != null)
        {
            expr.Update(argumentList);
        }

        return expr;
    }
}